package edi.controller;

import edi.api.Department;
import edi.api.Errors;
import edi.api.request.ChangeFileQuery;
import edi.api.response.DepListResponse;
import edi.api.response.GeneralResponse;
import edi.api.response.HistoryResponse;
import edi.dao.DepUsrDocDao;
import edi.dao.DepartmentDao;
import edi.dao.DepartmentUserDao;
import edi.dao.DocHistoryDao;
import edi.dao.DocumentDao;
import edi.dao.UserDao;
import edi.entities.DepUsrDocEntity;
import edi.entities.DepartmentEntity;
import edi.entities.DepartmentUserEntity;
import edi.entities.DocHistoryEntity;
import edi.entities.DocumentEntity;
import edi.entities.UserEntity;
import edi.manager.DocumentService;
import java.util.ArrayList;
import java.util.List;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("department")
public class DepartmentController {


    
    @Autowired
    private DepartmentDao depDao;
   
    
    
   
    
   
    
    @ResponseBody
    @RequestMapping(value = "/getDepartments", method = RequestMethod.GET)
    public DepListResponse getDepartments() {
        DepListResponse response = new DepListResponse();    
        
        try{
            
            List<DepartmentEntity> depEntities = depDao.findAll();
            List<Department> deps = new ArrayList<Department>(depEntities.size());
            
            for(DepartmentEntity depEnt : depEntities){
                Department dep = new Department();
                dep.setDepId(depEnt.getDepId());
                dep.setDepCode(depEnt.getDepCode());
                dep.setDepName(depEnt.getDepName());
                deps.add(dep);
            }
            
            response.setDepartments(deps);
  
        }catch(Exception e ){
            response.setErrorCode(Errors.GENERAL_ERROR);
            response.setErrorDesc(e.getLocalizedMessage());
        }
        
        return response;
    }
}
